﻿using System.IO;

namespace Aeon.Emulator.Dos
{
    /// <summary>
    /// A reference-counted stream.
    /// </summary>
    internal sealed class FileHandle
    {
        #region Constructors
        /// <summary>
        /// Initializes a new instance of the FileHandle class.
        /// </summary>
        /// <param name="stream">System.IO.Stream to wrap.</param>
        public FileHandle(Stream stream)
        {
            this.stream = stream;
            this.SFTIndex = -1;
        }
        #endregion

        #region Public Properties
        /// <summary>
        /// Gets the stream represented by the handle.
        /// </summary>
        public Stream Stream
        {
            get { return this.stream; }
        }
        /// <summary>
        /// Gets or sets the SFT index which refers to this file.
        /// </summary>
        public int SFTIndex { get; set; }
        /// <summary>
        /// Gets or sets additional information about the file.
        /// </summary>
        public VirtualFileInfo FileInfo { get; set; }
        #endregion

        #region Public Methods
        /// <summary>
        /// Adds a reference to the handle.
        /// </summary>
        public void AddReference()
        {
            this.referenceCount++;
        }
        /// <summary>
        /// Decrements the handle's reference count.
        /// </summary>
        /// <returns>True if stream has been closed; otherwise false.</returns>
        public bool Close()
        {
            if(this.referenceCount > 1)
            {
                this.referenceCount--;
                return false;
            }
            else if(this.referenceCount == 1)
            {
                this.referenceCount = 0;
                this.stream.Dispose();
            }

            return true;
        }
        #endregion

        #region Private Fields
        /// <summary>
        /// The backing stream for the handle.
        /// </summary>
        private Stream stream;
        /// <summary>
        /// The reference count of the handle.
        /// </summary>
        private int referenceCount = 1;
        #endregion
    }
}
